package com.longshihan.bei;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by longshihan on 2017/6/12.
 */

public class TouchPullView extends View {
    //圆
    Paint mCirclePaint;
    //半径
    private int mCircleRadius = 100;
    private int mCirclePaintX, mCirclePaintY;
    //进度值
    private float mProgress;
    //拖动的高度
    private int mDragHeight = 800;


    public TouchPullView(Context context) {
        super(context);
        init();
    }

    public TouchPullView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TouchPullView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {

        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        //设置抗锯齿
        p.setAntiAlias(true);
        //设置防抖动
        p.setDither(true);
        //设置填充方式
        p.setStyle(Paint.Style.FILL);
        p.setColor(0xFF000000);
        mCirclePaint = p;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawCircle(mCirclePaintX, mCirclePaintY, mCircleRadius, mCirclePaint);

    }

    /**
     * 当进行测量时触发
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);


        int iHeight = (int) ((mDragHeight * mProgress + 0.5f) + getPaddingBottom() +
                getPaddingTop());
        int iWidth = 2 * mCircleRadius + getPaddingLeft() + getPaddingRight();
        int mesureWidth, mesureHeight;

        if (widthMode == MeasureSpec.EXACTLY) {
            //确切的
            mesureWidth = width;
        } else if (widthMode == MeasureSpec.AT_MOST) {
            //最多的
            mesureWidth = Math.min(iWidth, width);
        } else {
            mesureWidth = iWidth;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            //确切的
            mesureHeight = height;
        } else if (heightMode == MeasureSpec.AT_MOST) {
            //最多的
            mesureHeight = Math.min(iHeight, height);
        } else {
            mesureHeight = iHeight;
        }

        //设置测量的高度，宽度
        setMeasuredDimension(mesureWidth, mesureHeight);

    }

    /**
     * 当大小改变时
     *
     * @param w
     * @param h
     * @param oldw
     * @param oldh
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mCirclePaintX = getWidth() >> 1;
        mCirclePaintY = getHeight() >> 1;

    }

    public void setProgress(float progress) {
        Log.d("Test:", progress + "");
        mProgress = progress;
        //请求重新测量
        requestLayout();

    }
}
